package com.vlin.controller.tuya;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.vlin.api.client.Client;
import com.vlin.api.client.Method;
import com.vlin.api.client.Params;
import com.vlin.service.tuya.TuyaControlService;

public class TuyaControlAPIController implements TuyaControlService {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private Client client;

	private static final String GET_CATEGORIES = "/v1.0/infrareds/%s/categories";
	
	@Override
	public String getSupportDevCategories(String devid) {
		
		if(devid == null)
			devid = "";
		return client.send(uri(GET_CATEGORIES,devid), Params.build(), Method.GET).toString();
	}
	
	private static final String SUPPORT_BRAND = "/v1.0/infrareds/%s/categories/%s/brands";

	@Override
	public String getCategorySupportBrands(String devid, String cid) {
		
		if(devid == null) {
			devid = "";
		}
		if(cid == null) {
			cid = "";
		}
		
		return client.send(uri(SUPPORT_BRAND, devid, cid), Params.build(), Method.GET).toString();
	}
	
	private static final String PROVINCES = "/v1.0/infrareds/%s/provinces";

	@Override
	public String getSupportProvinces(String devid) {
		checkParams(devid);
		
		return client.send(uri(PROVINCES, devid), Params.build(), Method.GET).toString();
	}
	
	private static final String CITIES = "/v1.0/infrareds/%s/provinces/%s/cities";

	@Override
	public String getProvinceCities(String devid, Integer pid) {
		checkParams(devid);
		return client.send(uri(CITIES, devid, String.valueOf(pid)), Params.build(), Method.GET).toString();
	}

	private static final String AREAS = "/v1.0/infrareds/%s/provinces/%s/cities/%s/areas";
	
	@Override
	public String getCityArea(String devid, Integer pid, Integer cityid) {
		checkParams(devid);
		return client.send(uri(AREAS, devid, String.valueOf(pid),String.valueOf(cityid)), Params.build(), Method.GET).toString();
	}

	private static final String AREA_BRANDS = "/v1.0/infrareds/%s/areas/%s/iptvs";
	
	@Override
	public String getAreaIptvs(String devid, String aid) {
		checkParams(devid, aid);
		
		return client.send(uri(AREA_BRANDS, devid, aid), Params.build(), Method.GET).toString();
	}
	
	private static final String OPERATOR_BRANDS = "/v1.0/infrareds/%s/operators/%s/brands?country_code=%s";

	@Override
	public String getBrandByCountryCode(String devid, String oid, String countryCode) {
		checkParams(devid,oid,countryCode);
		return client.send(uri(OPERATOR_BRANDS, devid, oid, countryCode), Params.build(), Method.GET).toString();
	}
	
	private static final String IPTV_INDEX = "/v1.0/infrareds/%s/operators/%s/brands/%s/iptvs";
	
	@Override
	public String getControlIndexByIptv(String devid, String oid, String bid) {
		checkParams(devid, oid, bid);
		return client.send(uri(IPTV_INDEX, devid, oid, bid), Params.build(), Method.GET).toString();
	}
	
	private static final String NO_IPTV_INDEX = "/v1.0/infrareds/%s/operators/%s/areas/%s";

	@Override
	public String getControlIndexByNoIptv(String devid, String oid, String aid) {
		checkParams(devid, oid, aid);
		return client.send(uri(NO_IPTV_INDEX, devid, oid, aid), Params.build(), Method.GET).toString();
	}

	private static final String BRAND_INDEX = "/v1.0/infrareds/%s/categories/%s/brands/%s";
	
	@Override
	public String getControlIndexByBand(String devid, String cid, String bid) {
		checkParams(devid, cid, bid);
		return client.send(uri(BRAND_INDEX, devid, cid, bid), Params.build(), Method.GET).toString();
	}
	
	private static final String BIND_LIST = "/v1.0/infrareds/%s/remotes";

	@Override
	public String getBindControl(String devid) {
		checkParams(devid);
		return client.send(uri(BIND_LIST, devid), Params.build(), Method.GET).toString();
	}
	
	private static final String ADD_REMOTE = "/v1.0/infrareds/%s/add-remote";

	@Override
	public String addControl(String devid, String body) {
		checkParams(devid, body);
		return client.send(uri(ADD_REMOTE, devid), Params.build().setParam("body", body), Method.POST).toString();
	}

	private static final String ADD_COMMON_REMOTE = "/v1.0/infrareds/%s/normal/add-remote";
	
	@Override
	public String addCommonControl(String devid, String body) {
		checkParams(devid, body);
		return client.send(uri(ADD_COMMON_REMOTE, devid), Params.build().setParam("body", body), Method.POST).toString();
	}

	@Override
	public String addSetTopBoxControl(String devid, String body) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delControl(String devid, String remoteId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String setControlName(String devid, String body) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getControlRules(String devid, String cid, String bid, String remoteIndex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getSomeKeyInfraredCode(String devid, String cid, String remoteIndex, String key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getALLInfraredCode(String devid, String cid, String remoteIndex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAirConditionInfraredCode(String devid, String remoteIndex, String power, String mode, String temp,
			String wind, String swing) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String sendInfraredCodeByKey(String devid, String body) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String sendInfraredCode(String devid, String remoteId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String sendAirConditionInfraredCode(String devid, String remoteId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAirConditionStatu(String devid, String remoteId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String controlLearning(String devid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String selectLearnedInfraredCode(String devid, String time) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String saveLearnedInfraredCode(String devid, String codes) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getLearnedInfraredCode(String devid, String remoteId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String updateLearned(String devid, String codes, String remoteId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String exitLearningMode(String devid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delLearnedInfraredCode(String devid, Long lid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String addInfraredCodeTimer(String devid, String body) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getInfraredCodeTimer(String devid, String cid, String remoteId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String updateInfraredCodeTimer(String devid, String gid, String body) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delTimerByGid(String devid, String gid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delAllTimer(String devid) {
		// TODO Auto-generated method stub
		return null;
	}
	


	
	private String uri(String format, String ...strings) {
		
		return String.format(format, strings);
	}
	
	private void checkParams(String...strings) {
		if(strings == null)
			return;
		for(String s : strings) {
			if(s == null) {
				s = "";
			}
		}
	}

}
